package com.exsun.gateway.jt809.kafka;

import com.exsun.gateway.jt809.common.DevInfoService;
import com.exsun.gateway.jt809.common.LinkMgr;
import com.exsun.gateway.jt809.common.LinkType;
import com.exsun.gateway.jt809.config.NettyClientConfig;
import com.exsun.gateway.jt809.data.PushData;
import com.exsun.gateway.jt809.handler.SendMgrHandler;
import com.google.gson.reflect.TypeToken;
import exsun.jt808.data.config.jt808.Jt808ReceiveMsgTopic;
import exsun.jt808.data.config.jt809.Jt809MsgIdConfig;
import exsun.jt808.data.jt809_data.BasicVehicleInfo;
import exsun.jt808.data.jt809_data.VehicleRegistrationInformation;
import exsun.jt808.data.jt809_data.vehicledynamic.main.UpBaseMsgVehicleVerify;
import exsun.jt808.data.jt809_data.vehicledynamic.main.UpBaseMsgVehicleVerifyData;
import exsun.jt808.data.jt809_data.vehicledynamic.main.UpExgMsgRegister;
import exsun.jt808.data.kafka_data.KafkaJT808Data;
import exsun.jt808.data.upload_data.jt808.TerminalAuthentication;
import exsun.jt808.data.util.GsonUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
@Service
public class JT809KafkaVehicleRegistrationHandler {


//    public class JT809KafkaAuthenticationHandler extends JT809KafkaHandleBase<TerminalAuthentication> {
        private Logger logger = LoggerFactory.getLogger(JT809KafkaVehicleRegistrationHandler.class);
        @Autowired
        private NettyClientConfig nettyClientConfig;

        @Autowired
        private DevInfoService devInfoService;

        @Autowired
        private SendMgrHandler sendMgrHandler;

        //订阅车辆注册数据,上传车辆注册信息
        @KafkaListener(topics = Jt808ReceiveMsgTopic.VehicleRegistrationInfor, containerFactory = "vehicleRegister809GroupFactory")
        public void batchVehicleRegistrationInforConsumerListen(List<ConsumerRecord<?, ?>> records, Acknowledgment ack) {
            try {
                List<PushData> temp = done(records);
                LinkMgr.push2FitChannel(temp, LinkType.MASTER);
                //发送错误，则不提交偏移量
                if(temp == null)return;
//                if(pushs.size() != 0){
//                    logger.info("上报注册：{}条", pushs.size());
//                }
                ack.acknowledge();
            }catch(Exception e){
                logger.info(e.getMessage());
            }
        }
        public List<PushData> done(List<ConsumerRecord<?, ?>> records) {
            Type type = new TypeToken<VehicleRegistrationInformation>() {
            }.getType();
            List<PushData> temp = new ArrayList<>();
            for (ConsumerRecord<?, ?> record : records) {
                //任意链路都不通，则不处理数据
                if (!LinkMgr.getLinkStatus()) return null;
                String msg = record.value().toString();
                VehicleRegistrationInformation object = GsonUtils.getObjectFromJson(msg, type);
                UpBaseMsgVehicleVerify upBaseMsgVehicleVerify = uploadRegisterInfo(object);
                byte[] body = sendMgrHandler.jt809Encode.upBaseMsgVehicleVerify(upBaseMsgVehicleVerify);
                PushData pushData = new PushData(Jt809MsgIdConfig.UP_BASE_MSG_VEHICLE_VERIFY, body);
                temp.add(pushData);
            }
            return temp;
        }
        /**
         * 上传车辆注册
         *
         */
        private UpBaseMsgVehicleVerify uploadRegisterInfo(VehicleRegistrationInformation object) {
            UpBaseMsgVehicleVerify upBaseMsgVehicleVerify = new UpBaseMsgVehicleVerify();
            upBaseMsgVehicleVerify.getBasicVehicleInfo().vehicleNo = object.getNumberPlate();
            upBaseMsgVehicleVerify.getBasicVehicleInfo().vehicleColor = object.getPlateColor();
            UpBaseMsgVehicleVerifyData content = upBaseMsgVehicleVerify.getContent();
            //content.setVerifyInfoType();
            content.setVin(object.getNumberPlate());
            content.setSimCode(object.getSimId());
            content.setTerminalId(object.getTerminalId());
            content.setOwnerName(object.getOwnerName());
            content.setOwnerTel(object.getContactNumber());
            content.setOwnerSex(object.getOwnerSexName());
            content.setTerminalType(object.getDeviceModelName());
            content.setProviderName(object.getOperatorName());
            content.setCompanyName(object.getCompanyName());
            content.setDriverName(object.getDriverNameOne());
            content.setDriverTel(object.getDriverContactNumberOne());
            content.setSubDriverName(object.getDriverNameTwo());
            content.setSubDriverTel(object.getDriverContactNumberTwo());
            content.setServiceStartTime(object.getServiceStartTime().getTime()+"");
            content.setOptType(object.getOperateTypeName());
            content.setDistrictCode(object.getCityCode()+"");
            content.setVehicleType(object.getVehicleTypeName());
            content.setOptRoute(object.getBusinessLine());
            content.setDeviceInstallTime(object.getEquipmentInstallTime().getTime()+"");
            return upBaseMsgVehicleVerify;
        }
//    }
}
